﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Web.Services.Protocols;

namespace Boomerang.Management.UIForms
{
    

    public partial class ExceptionBox : Form
    {
        private bool expanded = true;

        private ExceptionBox()
        {
            InitializeComponent();            
        }

        public ExceptionBox(Form parent, Exception exception, string title) : this()
        {
            string message = exception.Message;
            
            if ( ((exception as SoapException) != null) && ((SoapException)exception).Detail!=null)
            {
                message = message + "\r\n:Details: " + (exception as SoapException).Detail.InnerText;
            }
            else if ( (exception as WebException)!=null)
            {
                message = message + "\r\n:Status: " + (exception as WebException).Status;
            }

            labelExceptionMessage.Text = message;

            this.Text = parent.Text;

            labeTitle.Text = title;

            richTextBoxDetails.Text = exception.ToString();

            DetailsAction();
        }

        public ExceptionBox(Exception exception, string title)
            : this()
        {
            string message = exception.Message;
            if (exception is SoapException)
            {
                message = message + "\r\n:Details: " + (exception as SoapException).Detail.InnerXml;
            }
            else if (exception is WebException)
            {
                message = message + "\r\n:Status: " + (exception as WebException).Status;
            }

            labelExceptionMessage.Text = message;

            labeTitle.Text = title;

            richTextBoxDetails.Text = exception.ToString();

            DetailsAction();
        }

        public static void Show(Form owner, Exception exception, string title)
        {
            if (owner.InvokeRequired) ShowFromThread(owner, exception, title);
            new ExceptionBox(owner, exception, title).ShowDialog(owner);
        }

        public static void ShowFromThread(Form owner, Exception exception, string title)
        {
            new ExceptionBox(exception, title).ShowDialog();
        }

        public static void Show(Form owner, Exception exception)
        {
            Show(owner, exception, "An error ocurred.");
        }

        private void DetailsAction()
        {
            if (expanded)
            {
                //Collapsing Details
                expanded = false;

                this.richTextBoxDetails.Visible = false;
                this.richTextBoxDetails.Size = new Size(0, 0);
                this.Size = new Size(500, 200);
            }
            else
            {
                //Expanding Details
                expanded = true;
                
                this.richTextBoxDetails.Size = new Size(457, 148);
                this.Size = new Size(500, 335);
                this.richTextBoxDetails.Visible = true;
            }
        }

        private void buttonDetails_Click(object sender, EventArgs e)
        {
            DetailsAction();
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
